package cn.com.nes.site.service.system.company.impl;

import cn.com.nes.mybatis.agent.ope.entity.IsManufactureInfo;
import cn.com.nes.mybatis.agent.ope.mapper.IsManufactureInfoMapper;
import cn.com.nes.mybatis.agent.system.entity.Company;
import cn.com.nes.site.service.system.company.ManufactureService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ManufactureServiceImpl implements ManufactureService {

    @Resource
    private IsManufactureInfoMapper isManufactureInfoMapper;

    @Override
    public void saveOpeCompany(Company company, String username, boolean addCompany) throws Exception {
        IsManufactureInfo manufactureInfo = new IsManufactureInfo();

        if (addCompany) {
            IsManufactureInfo exist = isManufactureInfoMapper.getByManufactureId(company.getNesCompanyid());
            if (ObjectUtil.isNotNull(exist)) {
                throw new Exception("存在相同机构编码的公司");
            }
            manufactureInfo.setManufactureId(company.getCompanyid());
            manufactureInfo.setCjsj(company.getGmtCreate());
            manufactureInfo.setCrOper(username);
            manufactureInfo.setSfsc(0);
        } else {
            manufactureInfo = isManufactureInfoMapper.getByManufactureId(company.getNesCompanyid());
            if (ObjectUtil.isNull(manufactureInfo)) {
                throw new Exception("不存在manufacture公司!");
            }
            manufactureInfo.setXgsj(company.getGmtModified());
            manufactureInfo.setUpOper(username);
        }

        manufactureInfo.setAddress(company.getAddress());
        manufactureInfo.setRegisterAddress(company.getAddress());
        manufactureInfo.setName(company.getShortname());
        manufactureInfo.setFullName(company.getCompanyname());
        manufactureInfo.setContact(company.getContact());
        manufactureInfo.setContactPhone(company.getContactnumber());
        manufactureInfo.setManufactureMainAccount(company.getOrganizationCode());
        manufactureInfo.setCorporate(company.getLegalperson());
        manufactureInfo.setAreaNumber(company.getAreacode());
        manufactureInfo.setIsOpen(0);
        manufactureInfo.setIsAgent(0);
        manufactureInfo.setCompanyCode(company.getOrganizationCode());
        manufactureInfo.setIsOpenCustom(0);

        if (addCompany) {
            isManufactureInfoMapper.insert(manufactureInfo);
        } else {
            isManufactureInfoMapper.updateById(manufactureInfo);
        }
    }

    @Override
    public List<IsManufactureInfo> getManufactures(List<String> queryManufactureList) throws Exception {
        if (CollUtil.isEmpty(queryManufactureList)) {
            return Collections.emptyList();
        }
        QueryWrapper<IsManufactureInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sfsc", 0);
        queryWrapper.in("manufacture_id", queryManufactureList);
        return isManufactureInfoMapper.selectList(queryWrapper);
    }

}
